package com.gc.gpsoo.util;

/**
 * 将js常用的下划线语法的转换成Java的驼峰式
 *
 * @author 章瑱彬
 *         电话：15914921094
 *         QQ：562800930
 * @since 2015年3月31日
 */
public class JsJavaJsonFormatter {

    private static boolean isKey(StringBuilder sb, int index) {
        //判断是不是键，只要判断是不是在 ,和: 之间或者在 {和: 之间
        boolean beforeLQuotation = false;
        boolean beforeRQuotation = false;
        boolean afterLQuotation = false;
        boolean afterRQuotation = false;

        boolean beforeEqual = false;
        boolean afterEqual = false;
        boolean beforeColon = false;
        boolean beforeComma = false;
        boolean afterColon = false;
        boolean afterComma = false;

        int beforeIndex = index;
        int afterIndex = index;

        while (beforeIndex >= 0) {
            beforeIndex--;
            char ch = sb.charAt(beforeIndex);
            switch (ch) {
                case '{':
                    beforeLQuotation = true;
                    break;
                case '}':
                    beforeRQuotation = true;
                    break;
                case ',':
                    beforeComma = true;
                    break;
                case ':':
                    beforeColon = true;
                    break;
                case '=':
                    beforeEqual = true;
                    break;
            }

            if (beforeLQuotation || beforeComma)
                break;
            else if (beforeRQuotation || beforeColon || beforeEqual)
                return false;
        }
        if (!beforeLQuotation && !beforeComma) {
            return false;
        }

        while (afterIndex <= sb.length()) {
            afterIndex++;
            char ch = sb.charAt(afterIndex);
            switch (ch) {
                case '{':
                    afterLQuotation = true;
                    break;
                case '}':
                    afterRQuotation = true;
                    break;
                case ',':
                    afterComma = true;
                    break;
                case ':':
                    afterColon = true;
                    break;
                case '=':
                    afterEqual = true;
                    break;
            }
            if (afterLQuotation || afterComma)
                return false;
            else if (afterRQuotation || afterColon || afterEqual)
                return true;

        }
        return false;
    }

    public static String jsFormatterToJava(String json) {
        StringBuilder sb = new StringBuilder(json);
        int fromIndex = 0;
        int index;
        while ((index = sb.indexOf("_", fromIndex)) != -1) {
            if (!isKey(sb, index)) {
                System.out.println(sb.substring(index - 5, index + 1));
                System.out.println("_ at [" + index + "] is not a key!");
                fromIndex = index + 1;
            } else {
                sb.deleteCharAt(index);
                char ch = sb.charAt(index);
                if (ch >= 'a' && ch <= 'z') {
                    sb.setCharAt(index, (char) (ch - 32));
                }
                fromIndex = index;

            }
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        System.out.println(System.currentTimeMillis());
        String json = "{\"ret\":0,\"msg\":\"\",\"data\":[{\"imei\":\"862304020563717\",\"device_info\":0,\"device_info_new\":4,\"gps_time\":1427775152,\"sys_time\":1427775155,\"heart_time\":1427781439,\"server_time\":1427781667,\"lng\":116.135223,\"lat\":24.303029,\"course\":95,\"speed\":0,\"acc\":\"0\",\"seconds\":6512}]}";
        System.out.println(System.currentTimeMillis());
        String s = jsFormatterToJava(json);
        System.out.println(System.currentTimeMillis());
        System.out.println(json);
        System.out.println(s);
    }
}
